package pictureseditor.factories;

import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import pictureseditor.images.Image;
import pictureseditor.images.ImageImpl;


public class ImageResizer {

	public Image resize(Image image, double factor) {
		
		// new size
		int newWidth = (int) Math.sqrt((Math.pow(image.getLargeur(), 2))/factor);
		int newHeight = (int) Math.sqrt((Math.pow(image.getHauteur(), 2))/factor);
		
		// Image to BufferedImage
		BufferedImage bufferedImage = null;
		if(image.getProfondeur()==3) bufferedImage = fill3Layers(image);
		else if(image.getProfondeur()==1) bufferedImage = fill1Layer(image);
		
		// resize BufferedImage
        BufferedImage dimg = new BufferedImage(newWidth, newHeight, bufferedImage.getType());  
        Graphics2D g = dimg.createGraphics();  
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
        g.drawImage(bufferedImage, 0, 0, newWidth, newHeight, 0, 0, image.getLargeur(), image.getHauteur(), null);  
        g.dispose();  
		g.dispose();
		
		// BufferedImage to Image
		int largeur = bufferedImage.getData().getWidth();
	    int hauteur = bufferedImage.getData().getHeight();
	    int profondeur = 3;
	    Image result = new ImageImpl(3, largeur, hauteur);
		for(int l=0; l<largeur; l++) {
			for(int h=0; h<hauteur; h++) {

				int pixelValue = bufferedImage.getRGB(l, h);
				char R = (char) ((pixelValue & 0xFF0000)>>16) ;		// red value
				char G = (char) ((pixelValue & 0x00FF00)>>8) ;  	// green value
				char B = (char) (pixelValue & 0x0000FF) ;  			// blue value
				
				for(int p=0; p<profondeur; p++) {
					switch(p) {
						case 0: result.setPixel(p, l, h, R); break;
						case 1: result.setPixel(p, l, h, G); break;
						case 2: result.setPixel(p, l, h, B); break;
						default: break;
		}	}	}	}
		
		return result;
	}
	
	  /////////////
	 // PRIVATE //
	/////////////

	private BufferedImage fill1Layer(Image image) {
		int largeur = image.getLargeur();
		int hauteur = image.getHauteur();
		BufferedImage bufferedImage = new BufferedImage(largeur, hauteur, BufferedImage.TYPE_INT_RGB);
		for(int l=0; l<largeur; l++) {
			for(int h=0; h<hauteur; h++) {
				int pixel = image.getPixel(0, l, h);
				String sPixel;
				if(pixel<=15) sPixel = "0"+Integer.toHexString(pixel);
        		else sPixel = Integer.toHexString(pixel);
				bufferedImage.setRGB(l, h, Integer.parseInt(sPixel+sPixel+sPixel,16));
			}
		}
		return bufferedImage;
	}
	
	private BufferedImage fill3Layers(Image image) {
		int largeur = image.getLargeur();
		int hauteur = image.getHauteur();
		BufferedImage bufferedImage = new BufferedImage(largeur, hauteur, BufferedImage.TYPE_INT_RGB);
		for(int l=0; l<largeur; l++) {
			for(int h=0; h<hauteur; h++) {
				int R = image.getPixel(0, l, h);
				int G = image.getPixel(1, l, h);
				int B = image.getPixel(2, l, h);
				String sR, sG, sB;
				if(R<=15) sR = "0"+Integer.toHexString(R);
        		else sR = Integer.toHexString(R);
				if(G<=15) sG = "0"+Integer.toHexString(G);
        		else sG = Integer.toHexString(G);
				if(B<=15) sB = "0"+Integer.toHexString(B);
        		else sB = Integer.toHexString(B);
				bufferedImage.setRGB(l, h, Integer.parseInt(sR+sG+sB,16));
			}
		}
		return bufferedImage;
	}
	
}
